home *** CD-ROM | disk | FTP | other *** search
Wrap
# Source Generated with Decompyle++ # File: in.pyc (Python 2.6) from __future__ import generators import sys import os import fnmatch import tempfile import socket import struct import select import time import fcntl import errno import stat import string import commands import cStringIO import re import xml.parsers.expat as expat import getpass import locale import htmlentitydefs try: import platform platform_avail = True except ImportError: platform_avail = False from g import * from codes import * import pexpect BIG_ENDIAN = 0 LITTLE_ENDIAN = 1 def lock(f): log.debug('Locking: %s' % f.name) try: fcntl.flock(f.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB) return True except (IOError, OSError): log.debug('Failed to unlock %s.' % f.name) return False def unlock(f): if f is not None: log.debug('Unlocking: %s' % f.name) try: fcntl.flock(f.fileno(), fcntl.LOCK_UN) os.remove(f.name) except (IOError, OSError): pass except: None<EXCEPTION MATCH>(IOError, OSError) None<EXCEPTION MATCH>(IOError, OSError) def lock_app(application, suppress_error = False): dir = prop.user_dir if os.geteuid() == 0: dir = '/var' elif not os.path.exists(dir): os.makedirs(dir) lock_file = os.path.join(dir, '.'.join([ application, 'lock'])) try: lock_file_f = open(lock_file, 'w') except IOError: if not suppress_error: log.error('Unable to open %s lock file.' % lock_file) return (False, None) if not lock(lock_file_f): if not suppress_error: log.error('Unable to lock %s. Is %s already running?' % (lock_file, application)) return (False, None) return (True, lock_file_f) def Translator(frm = '', to = '', delete = '', keep = None): allchars = string.maketrans('', '') if len(to) == 1: to = to * len(frm) trans = string.maketrans(frm, to) if keep is not None: delete = allchars.translate(allchars, keep.translate(allchars, delete)) def callable(s): return s.translate(trans, delete) return callable def to_bool_str(s, default = '0'): ''' Convert an arbitrary 0/1/T/F/Y/N string to a normalized string 0/1.''' return default def to_bool(s, default = False): ''' Convert an arbitrary 0/1/T/F/Y/N string to a boolean True/False value.''' if isinstance(s, str) and s: if s[0].lower() in ('1', 't', 'y'): return True if s[0].lower() in ('0', 'f', 'n'): return False elif isinstance(s, bool): return s s[0].lower() in ('1', 't', 'y') return default def walkFiles(root, recurse = True, abs_paths = False, return_folders = False, pattern = '*', path = None): if path is None: path = root try: names = os.listdir(root) except os.error: raise StopIteration if not pattern: pass pattern = '*' pat_list = pattern.split(';') for name in names: fullname = os.path.normpath(os.path.join(root, name)) for pat in pat_list: if fnmatch.fnmatch(name, pat): if return_folders or not os.path.isdir(fullname): pass None if abs_paths else None<EXCEPTION MATCH>ValueError continue if recurse and os.path.isdir(fullname): for f in walkFiles(fullname, recurse, abs_paths, return_folders, pattern, path): yield f def is_path_writable(path): return False class TextFormatter: LEFT = 0 CENTER = 1 RIGHT = 2 def __init__(self, colspeclist): self.columns = [] for colspec in colspeclist: self.columns.append(Column(**colspec)) def compose(self, textlist, add_newline = False): numlines = 0 textlist = list(textlist) if len(textlist) != len(self.columns): log.error('Formatter: Number of text items does not match columns') return None for text, column in map(None, textlist, self.columns): column.wrap(text) numlines = max(numlines, len(column.lines)) complines = [ ''] * numlines for ln in range(numlines): for column in self.columns: complines[ln] = complines[ln] + column.getline(ln) if add_newline: return '\n'.join(complines) + '\n' return '\n'.join(complines) class Column: def __init__(self, width = 78, alignment = TextFormatter.LEFT, margin = 0): self.width = width self.alignment = alignment self.margin = margin self.lines = [] def align(self, line): if self.alignment == TextFormatter.CENTER: return line.center(self.width) if self.alignment == TextFormatter.RIGHT: return line.rjust(self.width) return line.ljust(self.width) def wrap(self, text): self.lines = [] words = [] for word in text.split(): if word <= self.width: words.append(word) continue for i in range(0, len(word), self.width): words.append(word[i:i + self.width]) if not len(words): return None current = words.pop(0) for word in words: increment = 1 + len(word) if len(current) + increment > self.width: self.lines.append(self.align(current)) current = word continue len(words) current = current + ' ' + word self.lines.append(self.align(current)) def getline(self, index): if index < len(self.lines): return ' ' * self.margin + self.lines[index] return ' ' * (self.margin + self.width) class Stack: def __init__(self): self.stack = [] def pop(self): return self.stack.pop() def push(self, value): self.stack.append(value) def as_list(self): return self.stack def clear(self): self.stack = [] def __len__(self): return len(self.stack) class Queue(Stack): def __init__(self): Stack.__init__(self) def get(self): return self.stack.pop(0) def put(self, value): Stack.push(self, value) class RingBuffer: def __init__(self, size_max = 50): self.max = size_max self.data = [] def append(self, x): '''append an element at the end of the buffer''' self.data.append(x) if len(self.data) == self.max: self.cur = 0 self.__class__ = RingBufferFull def replace(self, x): '''replace the last element instead off appending''' self.data[-1] = x def get(self): ''' return a list of elements from the oldest to the newest''' return self.data class RingBufferFull: def __init__(self, n): pass def append(self, x): self.data[self.cur] = x self.cur = (self.cur + 1) % self.max def replace(self, x): self.cur = (self.cur - 1) % self.max self.data[self.cur] = x self.cur = (self.cur + 1) % self.max def get(self): return self.data[self.cur:] + self.data[:self.cur] def sort_dict_by_value(d): ''' Returns the keys of dictionary d sorted by their values ''' items = d.items() backitems = [ [ v[1], v[0]] for v in items ] backitems.sort() return [ backitems[i][1] for i in range(0, len(backitems)) ] def commafy(val): return unicode(locale.format('%d', val, grouping = True)) def format_bytes(s, show_bytes = False): if s < 1024: return ''.join([ commafy(s), ' B']) if s < s: pass elif s < 1048576: if show_bytes: return ''.join([ unicode(round(s / 1024, 1)), u' KB (', commafy(s), ')']) return ''.join([ unicode(round(s / 1024, 1)), u' KB']) elif s < s: pass elif s < 1073741824: if show_bytes: return ''.join([ unicode(round(s / 1.04858e+06, 1)), u' MB (', commafy(s), ')']) return ''.join([ unicode(round(s / 1.04858e+06, 1)), u' MB']) elif show_bytes: return ''.join([ unicode(round(s / 1.07374e+09, 1)), u' GB (', commafy(s), ')']) s < 1024 return ''.join([ unicode(round(s / 1.07374e+09, 1)), u' GB']) try: make_temp_file = tempfile.mkstemp except AttributeError: def make_temp_file(suffix = '', prefix = '', dir = '', text = False): path = tempfile.mktemp(suffix) fd = os.open(path, os.O_RDWR | os.O_CREAT | os.O_EXCL, 448) return (os.fdopen(fd, 'w+b'), path) def which(command, return_full_path = False): path = os.getenv('PATH').split(':') path.append('/sbin') path.append('/usr/sbin') path.append('/usr/local/sbin') found_path = '' for p in path: try: files = os.listdir(p) except OSError: continue continue if command in files: found_path = p break continue if return_full_path: if found_path: return os.path.join(found_path, command) return '' return_full_path return found_path class UserSettings(object): def __init__(self): self.load() def loadDefaults(self): self.cmd_print = '' path = which('hp-print') if len(path) > 0: self.cmd_print = 'hp-print -p%PRINTER%' else: path = which('kprinter') if len(path) > 0: self.cmd_print = 'kprinter -P%PRINTER% --system cups' else: path = which('gtklp') if len(path) > 0: self.cmd_print = 'gtklp -P%PRINTER%' else: path = which('xpp') if len(path) > 0: self.cmd_print = 'xpp -P%PRINTER%' self.cmd_scan = '' path = which('xsane') if len(path) > 0: self.cmd_scan = 'xsane -V %SANE_URI%' else: path = which('kooka') if len(path) > 0: self.cmd_scan = 'kooka' else: path = which('xscanimage') if len(path) > 0: self.cmd_scan = 'xscanimage' path = which('hp-unload') if len(path): self.cmd_pcard = 'hp-unload -d %DEVICE_URI%' else: self.cmd_pcard = 'python %HOME%/unload.py -d %DEVICE_URI%' path = which('hp-makecopies') if len(path): self.cmd_copy = 'hp-makecopies -d %DEVICE_URI%' else: self.cmd_copy = 'python %HOME%/makecopies.py -d %DEVICE_URI%' path = which('hp-sendfax') if len(path): self.cmd_fax = 'hp-sendfax -d %FAX_URI%' else: self.cmd_fax = 'python %HOME%/sendfax.py -d %FAX_URI%' path = which('hp-fab') if len(path): self.cmd_fab = 'hp-fab' else: self.cmd_fab = 'python %HOME%/fab.py' def load(self): self.loadDefaults() log.debug('Loading user settings...') self.auto_refresh = to_bool(user_conf.get('refresh', 'enable', '0')) try: self.auto_refresh_rate = int(user_conf.get('refresh', 'rate', '30')) except ValueError: self.auto_refresh_rate = 30 try: self.auto_refresh_type = int(user_conf.get('refresh', 'type', '0')) except ValueError: self.auto_refresh_type = 0 self.cmd_print = user_conf.get('commands', 'prnt', self.cmd_print) self.cmd_scan = user_conf.get('commands', 'scan', self.cmd_scan) self.cmd_pcard = user_conf.get('commands', 'pcard', self.cmd_pcard) self.cmd_copy = user_conf.get('commands', 'cpy', self.cmd_copy) self.cmd_fax = user_conf.get('commands', 'fax', self.cmd_fax) self.cmd_fab = user_conf.get('commands', 'fab', self.cmd_fab) self.debug() def debug(self): log.debug('Print command: %s' % self.cmd_print) log.debug('PCard command: %s' % self.cmd_pcard) log.debug('Fax command: %s' % self.cmd_fax) log.debug('FAB command: %s' % self.cmd_fab) log.debug('Copy command: %s ' % self.cmd_copy) log.debug('Scan command: %s' % self.cmd_scan) log.debug('Auto refresh: %s' % self.auto_refresh) log.debug('Auto refresh rate: %s' % self.auto_refresh_rate) log.debug('Auto refresh type: %s' % self.auto_refresh_type) def save(self): log.debug('Saving user settings...') user_conf.set('commands', 'prnt', self.cmd_print) user_conf.set('commands', 'pcard', self.cmd_pcard) user_conf.set('commands', 'fax', self.cmd_fax) user_conf.set('commands', 'scan', self.cmd_scan) user_conf.set('commands', 'cpy', self.cmd_copy) user_conf.set('refresh', 'enable', self.auto_refresh) user_conf.set('refresh', 'rate', self.auto_refresh_rate) user_conf.set('refresh', 'type', self.auto_refresh_type) self.debug() def no_qt_message_gtk(): try: import gtk w = gtk.Window() dialog = gtk.MessageDialog(w, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_WARNING, gtk.BUTTONS_OK, 'PyQt not installed. GUI not available. Please check that the PyQt package is installed. Exiting.') dialog.run() dialog.destroy() except ImportError: log.error('PyQt not installed. GUI not available. Please check that the PyQt package is installed. Exiting.') def canEnterGUIMode(): if not prop.gui_build: log.warn('GUI mode disabled in build.') return False if not os.getenv('DISPLAY'): log.warn('No display found.') return False if not checkPyQtImport(): log.warn('Qt/PyQt 3 initialization failed.') return False return True def canEnterGUIMode4(): if not prop.gui_build: log.warn('GUI mode disabled in build.') return False if not os.getenv('DISPLAY'): log.warn('No display found.') return False if not checkPyQtImport4(): log.warn('Qt/PyQt 4 initialization failed.') return False return True def checkPyQtImport(): try: import qt except ImportError: if os.getenv('DISPLAY') and os.getenv('STARTED_FROM_MENU'): no_qt_message_gtk() log.error('PyQt not installed. GUI not available. Exiting.') return False qtMajor = int(qt.qVersion().split('.')[0]) if qtMajor < MINIMUM_QT_MAJOR_VER: log.error('Incorrect version of Qt installed. Ver. 3.0.0 or greater required.') return False try: pyqtVersion = qt.PYQT_VERSION_STR except AttributeError: qtMajor < MINIMUM_QT_MAJOR_VER qtMajor < MINIMUM_QT_MAJOR_VER pyqtVersion = qt.PYQT_VERSION except: qtMajor < MINIMUM_QT_MAJOR_VER while pyqtVersion.count('.') < 2: pyqtVersion += '.0' continue qtMajor < MINIMUM_QT_MAJOR_VER (maj_ver, min_ver, pat_ver) = pyqtVersion.split('.') return True def checkPyQtImport4(): try: import PyQt4 except ImportError: return False return True try: from string import Template except ImportError: class _multimap: '''Helper class for combining multiple mappings. Used by .{safe_,}substitute() to combine the mapping and keyword arguments. ''' def __init__(self, primary, secondary): self._primary = primary self._secondary = secondary def __getitem__(self, key): try: return self._primary[key] except KeyError: return self._secondary[key] class _TemplateMetaclass(type): pattern = '\n %(delim)s(?:\n (?P<escaped>%(delim)s) | # Escape sequence of two delimiters\n (?P<named>%(id)s) | # delimiter and a Python identifier\n {(?P<braced>%(id)s)} | # delimiter and a braced identifier\n (?P<invalid>) # Other ill-formed delimiter exprs\n )\n ' def __init__(cls, name, bases, dct): super(_TemplateMetaclass, cls).__init__(name, bases, dct) if 'pattern' in dct: pattern = cls.pattern else: pattern = _TemplateMetaclass.pattern % { 'delim': re.escape(cls.delimiter), 'id': cls.idpattern } cls.pattern = re.compile(pattern, re.IGNORECASE | re.VERBOSE) class Template: '''A string class for supporting $-substitutions.''' __metaclass__ = _TemplateMetaclass delimiter = '$' idpattern = '[_a-z][_a-z0-9]*' def __init__(self, template): self.template = template def _invalid(self, mo): i = mo.start('invalid') lines = self.template[:i].splitlines(True) if not lines: colno = 1 lineno = 1 else: colno = i - len(''.join(lines[:-1])) lineno = len(lines) raise ValueError('Invalid placeholder in string: line %d, col %d' % (lineno, colno)) def substitute(self, *args, **kws): if len(args) > 1: raise TypeError('Too many positional arguments') len(args) > 1 if not args: mapping = kws elif kws: mapping = _multimap(kws, args[0]) else: mapping = args[0] def convert(mo): if not mo.group('named'): pass named = mo.group('braced') if named is not None: val = mapping[named] return '%s' % val if mo.group('escaped') is not None: return self.delimiter raise ValueError('Unrecognized named group in pattern', self.pattern) return self.pattern.sub(convert, self.template) def safe_substitute(self, *args, **kws): if len(args) > 1: raise TypeError('Too many positional arguments') len(args) > 1 if not args: mapping = kws elif kws: mapping = _multimap(kws, args[0]) else: mapping = args[0] def convert(mo): named = mo.group('named') if named is not None: try: return '%s' % mapping[named] except KeyError: return self.delimiter + named None<EXCEPTION MATCH>KeyError braced = mo.group('braced') if braced is not None: try: return '%s' % mapping[braced] except KeyError: return self.delimiter + '{' + braced + '}' None<EXCEPTION MATCH>KeyError if mo.group('escaped') is not None: return self.delimiter if mo.group('invalid') is not None: return self.delimiter raise ValueError('Unrecognized named group in pattern', self.pattern) return self.pattern.sub(convert, self.template) def cat(s): globals = sys._getframe(1).f_globals.copy() if 'self' in globals: del globals['self'] locals = sys._getframe(1).f_locals.copy() if 'self' in locals: del locals['self'] return Template(s).substitute(sys._getframe(1).f_globals, **locals) identity = string.maketrans('', '') unprintable = identity.translate(identity, string.printable) def printable(s): return s.translate(identity, unprintable) def any(S, f = (lambda x: x)): for x in S: if f(x): return True return False def all(S, f = (lambda x: x)): for x in S: if not f(x): return False return True BROWSERS = [ 'firefox', 'mozilla', 'konqueror', 'galeon', 'skipstone'] BROWSER_OPTS = { 'firefox': '-new-window', 'mozilla': '', 'konqueror': '', 'galeon': '-w', 'skipstone': '' } def find_browser(): if platform_avail and platform.system() == 'Darwin': return 'open' for b in BROWSERS: if which(b): return b else: return None return which(b) def openURL(url, use_browser_opts = True): if platform_avail and platform.system() == 'Darwin': cmd = 'open "%s"' % url log.debug(cmd) os.system(cmd) else: for b in BROWSERS: bb = which(b) if bb: bb = os.path.join(bb, b) if use_browser_opts: cmd = '%s %s "%s" &' % (bb, BROWSER_OPTS[b], url) else: cmd = '%s "%s" &' % (bb, url) log.debug(cmd) os.system(cmd) break continue def uniqueList(input): temp = [] _[1] return temp def list_move_up(l, m, cmp = None): for i in range(1, len(l)): if f(i): l[i - 1] = l[i] l[i] = l[i - 1] continue None if cmp is None else (None, None, None) def list_move_down(l, m, cmp = None): for i in range(len(l) - 2, -1, -1): if f(i): l[i] = l[i + 1] l[i + 1] = l[i] continue None if cmp is None else (None, None, None) class XMLToDictParser: def __init__(self): self.stack = [] self.data = { } self.last_start = '' def startElement(self, name, attrs): self.stack.append(unicode(name).lower()) self.last_start = unicode(name).lower() if len(attrs): for a in attrs: self.stack.append(unicode(a).lower()) self.addData(attrs[a]) self.stack.pop() def endElement(self, name): if name.lower() == self.last_start: self.addData('') self.stack.pop() def charData(self, data): data = unicode(data).strip() if data and self.stack: self.addData(data) def addData(self, data): self.last_start = '' try: data = int(data) except ValueError: data = unicode(data) stack_str = '-'.join(self.stack) stack_str_0 = '-'.join([ stack_str, '0']) try: self.data[stack_str] except KeyError: try: self.data[stack_str_0] except KeyError: self.data[stack_str] = data j = 2 while True: try: self.data['-'.join([ stack_str, unicode(j)])] except KeyError: self.data['-'.join([ stack_str, unicode(j)])] = data break j += 1 self.data[stack_str_0] = self.data[stack_str] self.data['-'.join([ stack_str, '1'])] = data del self.data[stack_str] def parseXML(self, text): parser = expat.ParserCreate() parser.StartElementHandler = self.startElement parser.EndElementHandler = self.endElement parser.CharacterDataHandler = self.charData parser.Parse(text.encode('utf-8'), True) return self.data def dquote(s): return ''.join([ '"', s, '"']) if sys.hexversion < 33686512: def xlstrip(s, chars = ' '): i = 0 for c, i in zip(s, range(len(s))): if c not in chars: break continue return s[i:] def xrstrip(s, chars = ' '): return xreverse(xlstrip(xreverse(s), chars)) def xreverse(s): l = list(s) l.reverse() return ''.join(l) def xstrip(s, chars = ' '): return xreverse(xlstrip(xreverse(xlstrip(s, chars)), chars)) else: xlstrip = string.lstrip xrstrip = string.rstrip xstrip = string.strip def getBitness(): if platform_avail: return int(platform.architecture()[0][:-3]) return struct.calcsize('P') << 3 def getProcessor(): if platform_avail: return platform.machine().replace(' ', '_').lower() return 'i686' def getEndian(): if sys.byteorder == 'big': return BIG_ENDIAN return LITTLE_ENDIAN def get_password(): return getpass.getpass('Enter password: ') def run(cmd, log_output = True, password_func = get_password, timeout = 1): output = cStringIO.StringIO() try: child = pexpect.spawn(cmd, timeout = timeout) except pexpect.ExceptionPexpect: return (-1, '') try: while True: update_spinner() i = child.expect([ '[pP]assword:', pexpect.EOF, pexpect.TIMEOUT]) if child.before: output.write(child.before) if log_output: log.debug(child.before) if i == 0: if password_func is not None: child.sendline(password_func()) else: child.sendline(get_password()) password_func is not None if i == 1: break continue if i == 2: continue continue except Exception: e = None log.error('Exception: %s' % e) cleanup_spinner() child.close() return (child.exitstatus, output.getvalue()) def expand_range(ns): '''Credit: Jean Brouwers, comp.lang.python 16-7-2004 Convert a string representation of a set of ranges into a list of ints, e.g. u"1-4, 7, 9-12" --> [1,2,3,4,7,9,10,11,12] ''' fs = [] for n in ns.split(u','): n = n.strip() r = n.split('-') if len(r) == 2: h = r[0].rstrip(u'0123456789') r[0] = r[0][len(h):] if not r[0] and r[1]: raise ValueError, 'empty range: ' + n r[1] r = [ int(i, 10) for i in r ] if r[0] > r[1]: raise ValueError, 'bad range: ' + n r[0] > r[1] for i in range(r[0], r[1] + 1): fs.append(h % i) [] fs.append(n) fs = []([ (n, i) for i, n in enumerate(fs) ]).keys() fs = _[4] fs.sort() return fs def collapse_range(x): ''' Convert a list of integers into a string range representation: [1,2,3,4,7,9,10,11,12] --> u"1-4,7,9-12" ''' if not x: return '' s = [ str(x[0])] c = x[0] r = False for i in x[1:]: if i == c + 1: r = True elif r: s.append(u'-%s,%s' % (c, i)) r = False else: s.append(u',%s' % i) c = i if r: s.append(u'-%s' % i) return ''.join(s) def createSequencedFilename(basename, ext, dir = None, digits = 3): if dir is None: dir = os.getcwd() m = 0 for f in walkFiles(dir, recurse = False, abs_paths = False, return_folders = False, pattern = '*', path = None): (r, e) = os.path.splitext(f) if r.startswith(basename) and ext == e: try: i = int(r[len(basename):]) except ValueError: continue m = max(m, i) continue return os.path.join(dir, '%s%0*d%s' % (basename, digits, m + 1, ext)) def validate_language(lang, default = 'en_US'): if lang is None: (loc, encoder) = locale.getdefaultlocale() else: lang = lang.lower().strip() for loc, ll in supported_locales.items(): if lang in ll: break continue else: loc = 'en_US' return loc def gen_random_uuid(): try: import uuid return str(uuid.uuid4()) except ImportError: uuidgen = which('uuidgen') if uuidgen: uuidgen = os.path.join(uuidgen, 'uuidgen') return commands.getoutput(uuidgen) return '' except: uuidgen class RestTableFormatter(object): def __init__(self, header = None): self.header = header self.rows = [] def add(self, row_data): self.rows.append(row_data) def output(self, w): if self.rows: num_cols = len(self.rows[0]) for r in self.rows: if len(r) != num_cols: log.error('Invalid number of items in row: %s' % r) return None if len(self.header) != num_cols: log.error('Invalid number of items in header.') col_widths = [] for x, c in enumerate(self.header): max_width = len(c) for r in self.rows: max_width = max(max_width, len(r[x])) col_widths.append(max_width + 2) x = '+' for c in col_widths: x = ''.join([ x, '-' * (c + 2), '+']) x = ''.join([ x, '\n']) w.write(x) if self.header: x = '|' for i, c in enumerate(col_widths): x = ''.join([ x, ' ', self.header[i], ' ' * (c + 1 - len(self.header[i])), '|']) x = ''.join([ x, '\n']) w.write(x) x = '+' for c in col_widths: x = ''.join([ x, '=' * (c + 2), '+']) x = ''.join([ x, '\n']) w.write(x) for j, r in enumerate(self.rows): x = '|' for i, c in enumerate(col_widths): x = ''.join([ x, ' ', self.rows[j][i], ' ' * (c + 1 - len(self.rows[j][i])), '|']) x = ''.join([ x, '\n']) w.write(x) x = '+' for c in col_widths: x = ''.join([ x, '-' * (c + 2), '+']) x = ''.join([ x, '\n']) w.write(x) else: log.error('No data rows') def mixin(cls): import inspect locals = inspect.stack()[1][0].f_locals if '__module__' not in locals: raise TypeError('Must call mixin() from within class def.') '__module__' not in locals dict = cls.__dict__.copy() dict.pop('__doc__', None) dict.pop('__module__', None) locals.update(dict) USAGE_OPTIONS = ('[OPTIONS]', '', 'heading', False) USAGE_LOGGING1 = ('Set the logging level:', '-l<level> or --logging=<level>', 'option', False) USAGE_LOGGING2 = ('', '<level>: none, info\\*, error, warn, debug (\\*default)', 'option', False) USAGE_LOGGING3 = ('Run in debug mode:', '-g (same as option: -ldebug)', 'option', False) USAGE_LOGGING_PLAIN = ('Output plain text only:', '-t', 'option', False) USAGE_ARGS = ('[PRINTER|DEVICE-URI]', '', 'heading', False) USAGE_ARGS2 = ('[PRINTER]', '', 'heading', False) USAGE_DEVICE = ('To specify a device-URI:', '-d<device-uri> or --device=<device-uri>', 'option', False) USAGE_PRINTER = ('To specify a CUPS printer:', '-p<printer> or --printer=<printer>', 'option', False) USAGE_BUS1 = ('Bus to probe (if device not specified):', '-b<bus> or --bus=<bus>', 'option', False) USAGE_BUS2 = ('', '<bus>: cups\\*, usb\\*, net, bt, fw, par\\* (\\*defaults) (Note: bt and fw not supported in this release.)', 'option', False) USAGE_HELP = ('This help information:', '-h or --help', 'option', True) USAGE_SPACE = ('', '', 'space', False) USAGE_EXAMPLES = ('Examples:', '', 'heading', False) USAGE_NOTES = ('Notes:', '', 'heading', False) USAGE_STD_NOTES1 = ('If device or printer is not specified, the local device bus is probed and the program enters interactive mode.', '', 'note', False) USAGE_STD_NOTES2 = ('If -p\\* is specified, the default CUPS printer will be used.', '', 'note', False) USAGE_SEEALSO = ('See Also:', '', 'heading', False) USAGE_LANGUAGE = ('Set the language:', '-q <lang> or --lang=<lang>. Use -q? or --lang=? to see a list of available language codes.', 'option', False) USAGE_LANGUAGE2 = ('Set the language:', '--lang=<lang>. Use --lang=? to see a list of available language codes.', 'option', False) USAGE_MODE = ('[MODE]', '', 'header', False) USAGE_NON_INTERACTIVE_MODE = ('Run in non-interactive mode:', '-n or --non-interactive', 'option', False) USAGE_GUI_MODE = ('Run in graphical UI mode:', '-u or --gui (Default)', 'option', False) USAGE_INTERACTIVE_MODE = ('Run in interactive mode:', '-i or --interactive', 'option', False) if sys_conf.get('configure', 'ui-toolkit', 'qt3') == 'qt3': USAGE_USE_QT3 = ('Use Qt3:', '--qt3 (Default)', 'option', False) USAGE_USE_QT4 = ('Use Qt4:', '--qt4', 'option', False) else: USAGE_USE_QT3 = ('Use Qt3:', '--qt3', 'option', False) USAGE_USE_QT4 = ('Use Qt4:', '--qt4 (Default)', 'option', False) def ttysize(): ln1 = commands.getoutput('stty -a').splitlines()[0] vals = { 'rows': None, 'columns': None } for ph in ln1.split(';'): x = ph.split() if len(x) == 2: vals[x[0]] = x[1] vals[x[1]] = x[0] continue try: rows = int(vals['rows']) cols = int(vals['columns']) except TypeError: (rows, cols) = (25, 80) return (rows, cols) def usage_formatter(override = 0): (rows, cols) = ttysize() if override: col1 = override col2 = cols - col1 - 8 else: col1 = int(cols / 3) - 8 col2 = cols - col1 - 8 return TextFormatter(({ 'width': col1, 'margin': 2 }, { 'width': col2, 'margin': 2 })) def format_text(text_list, typ = 'text', title = '', crumb = '', version = ''): ''' Format usage text in multiple formats: text: for --help in the console rest: for conversion with rst2web for the website man: for manpages ''' if typ == 'text': formatter = usage_formatter() for line in text_list: (text1, text2, format, trailing_space) = line text1 = text1.replace('\\', '') text2 = text2.replace('\\', '') if format == 'summary': log.info(log.bold(text1)) log.info('') continue if format in ('para', 'name', 'seealso'): log.info(text1) if trailing_space: log.info('') trailing_space if format in ('heading', 'header'): log.info(log.bold(text1)) continue if format in ('option', 'example'): log.info(formatter.compose((text1, text2), trailing_space)) continue if format == 'note': if text1.startswith(' '): log.info('\t' + text1.lstrip()) else: log.info(text1) text1.startswith(' ') if format == 'space': log.info('') continue log.info('') elif typ == 'rest': (opt_colwidth1, opt_colwidth2) = (0, 0) (exmpl_colwidth1, exmpl_colwidth2) = (0, 0) (note_colwidth1, note_colwidth2) = (0, 0) for line in text_list: (text1, text2, format, trailing_space) = line if format == 'option': opt_colwidth1 = max(len(text1), opt_colwidth1) opt_colwidth2 = max(len(text2), opt_colwidth2) continue if format == 'example': exmpl_colwidth1 = max(len(text1), exmpl_colwidth1) exmpl_colwidth2 = max(len(text2), exmpl_colwidth2) continue if format == 'note': note_colwidth1 = max(len(text1), note_colwidth1) note_colwidth2 = max(len(text2), note_colwidth2) continue opt_colwidth1 += 4 opt_colwidth2 += 4 exmpl_colwidth1 += 4 exmpl_colwidth2 += 4 note_colwidth1 += 4 note_colwidth2 += 4 opt_tablewidth = opt_colwidth1 + opt_colwidth2 exmpl_tablewidth = exmpl_colwidth1 + exmpl_colwidth2 note_tablewidth = note_colwidth1 + note_colwidth2 log.info('restindex\npage-title: %s\ncrumb: %s\nformat: rest\nfile-extension: html\nencoding: utf8\n/restindex\n' % (title, crumb)) t = '%s: %s (ver. %s)' % (crumb, title, version) log.info(t) log.info('=' * len(t)) log.info('') links = [] needs_header = False for line in text_list: (text1, text2, format, trailing_space) = line if format == 'seealso': links.append(text1) text1 = '`%s`_' % text1 len1 = len(text1) len2 = len(text2) if format == 'summary': log.info(''.join([ '**', text1, '**'])) log.info('') continue if format in ('para', 'name'): log.info('') log.info(text1) log.info('') continue if format in ('heading', 'header'): log.info('') log.info('**' + text1 + '**') log.info('') needs_header = True continue if format == 'option': if needs_header: log.info('.. class:: borderless') log.info('') log.info(''.join([ '+', '-' * opt_colwidth1, '+', '-' * opt_colwidth2, '+'])) needs_header = False if text1 and '`_' not in text1: log.info(''.join([ '| *', text1, '*', ' ' * (opt_colwidth1 - len1 - 3), '|', text2, ' ' * (opt_colwidth2 - len2), '|'])) elif text1: log.info(''.join([ '|', text1, ' ' * (opt_colwidth1 - len1), '|', text2, ' ' * (opt_colwidth2 - len2), '|'])) else: log.info(''.join([ '|', ' ' * opt_colwidth1, '|', text2, ' ' * (opt_colwidth2 - len2), '|'])) log.info(''.join([ '+', '-' * opt_colwidth1, '+', '-' * opt_colwidth2, '+'])) continue if format == 'example': if needs_header: log.info('.. class:: borderless') log.info('') log.info(''.join([ '+', '-' * exmpl_colwidth1, '+', '-' * exmpl_colwidth2, '+'])) needs_header = False if text1 and '`_' not in text1: log.info(''.join([ '| *', text1, '*', ' ' * (exmpl_colwidth1 - len1 - 3), '|', text2, ' ' * (exmpl_colwidth2 - len2), '|'])) elif text1: log.info(''.join([ '|', text1, ' ' * (exmpl_colwidth1 - len1), '|', text2, ' ' * (exmpl_colwidth2 - len2), '|'])) else: log.info(''.join([ '|', ' ' * exmpl_colwidth1, '|', text2, ' ' * (exmpl_colwidth2 - len2), '|'])) log.info(''.join([ '+', '-' * exmpl_colwidth1, '+', '-' * exmpl_colwidth2, '+'])) continue if format == 'seealso': if text1 and '`_' not in text1: log.info(text1) '`_' not in text1 if format == 'note': if needs_header: log.info('.. class:: borderless') log.info('') log.info(''.join([ '+', '-' * note_colwidth1, '+', '-' * note_colwidth2, '+'])) needs_header = False if text1.startswith(' '): log.info(''.join([ '|', ' ' * (note_tablewidth + 1), '|'])) log.info(''.join([ '|', text1, ' ' * ((note_tablewidth - len1) + 1), '|'])) log.info(''.join([ '+', '-' * note_colwidth1, '+', '-' * note_colwidth2, '+'])) continue if format == 'space': log.info('') continue for l in links: log.info('\n.. _`%s`: %s.html\n' % (l, l.replace('hp-', ''))) log.info('') elif typ == 'man': log.info('.TH "%s" 1 "%s" Linux "User Manuals"' % (crumb, version)) log.info('.SH NAME\n%s \\- %s' % (crumb, title)) for line in text_list: (text1, text2, format, trailing_space) = line text1 = text1.replace('\\*', '*') text2 = text2.replace('\\*', '*') len1 = len(text1) len2 = len(text2) if format == 'summary': log.info('.SH SYNOPSIS') log.info('.B %s' % text1.replace('Usage:', '')) continue if format == 'name': log.info('.SH DESCRIPTION\n%s' % text1) continue if format in ('option', 'example', 'note'): if text1: log.info('.IP "%s"\n%s' % (text1, text2)) else: log.info(text2) text1 if format in ('header', 'heading'): log.info('.SH %s' % text1.upper().replace(':', '').replace('[', '').replace(']', '')) continue if format in 'seealso, para': log.info(text1) continue log.info('.SH AUTHOR') log.info('HPLIP (Hewlett-Packard Linux Imaging and Printing) is an') log.info('HP developed solution for printing, scanning, and faxing with') log.info('HP inkjet and laser based printers in Linux.') log.info('.SH REPORTING BUGS') log.info('The HPLIP Launchpad.net site') log.info('.B https://launchpad.net/hplip') log.info('is available to get help, report') log.info('bugs, make suggestions, discuss the HPLIP project or otherwise') log.info('contact the HPLIP Team.') log.info('.SH COPYRIGHT') log.info('Copyright (c) 2001-9 Hewlett-Packard Development Company, L.P.') log.info('.LP') log.info('This software comes with ABSOLUTELY NO WARRANTY.') log.info('This is free software, and you are welcome to distribute it') log.info('under certain conditions. See COPYING file for more details.') log.info('') def log_title(program_name, version, show_ver = True): log.info('') if show_ver: log.info(log.bold('HP Linux Imaging and Printing System (ver. %s)' % prop.version)) else: log.info(log.bold('HP Linux Imaging and Printing System')) log.info(log.bold('%s ver. %s' % (program_name, version))) log.info('') log.info('Copyright (c) 2001-9 Hewlett-Packard Development Company, LP') log.info('This software comes with ABSOLUTELY NO WARRANTY.') log.info('This is free software, and you are welcome to distribute it') log.info('under certain conditions. See COPYING file for more details.') log.info('') def ireplace(old, search, replace): regex = '(?i)' + re.escape(search) return re.sub(regex, replace, old) def su_sudo(): su_sudo_str = None if which('kdesu'): su_sudo_str = 'kdesu -- %s' elif utils.which('/usr/lib/kde4/libexec/kdesu'): su_sudo_str = '/usr/lib/kde4/libexec/kdesu -- %s' elif utils.which('kdesudo'): su_sudo_str = 'kdesudo -- %s' elif which('gnomesu'): su_sudo_str = 'gnomesu -c "%s"' elif which('gksu'): su_sudo_str = 'gksu "%s"' return su_sudo_str def unescape(text): def fixup(m): text = m.group(0) if text[:2] == '': try: if text[:3] == '': return chr(int(text[3:-1], 16)) return chr(int(text[2:-1])) except ValueError: pass except: None<EXCEPTION MATCH>ValueError None<EXCEPTION MATCH>ValueError try: text = chr(htmlentitydefs.name2codepoint[text[1:-1]]) except KeyError: pass return text return re.sub('?\\w+;', fixup, text) def escape(s): if not isinstance(s, unicode): s = unicode(s) s = s.replace(u'&', u'&') for c in htmlentitydefs.codepoint2name: if c != 38: s = s.replace(unichr(c), u'&%s;' % htmlentitydefs.codepoint2name[c]) continue for c in range(32) + range(127, 160): s = s.replace(unichr(c), u'%d;' % c) return s